/**
 * 
 */
package plataforma.cih.botoes;

import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;

import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.Button;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Separator;
import org.zkoss.zul.Vbox;

/**
 * @author mike
 * 
 */
public class BotoesOrdenacao extends Vbox {

	Button btTopo = new Button();
	Button btBase = new Button();
	Button btSubir = new Button();
	Button btDescer = new Button();
	private Listbox listbox;
	
	public BotoesOrdenacao(Listbox listbox){
		configurar();
		this.listbox = listbox;
	}

	/**
	 * @return the listbox
	 */
	Listbox getListbox() {
		return listbox;
	}

	/**
	 * @param listbox the listbox to set
	 */
	void setListbox(Listbox listbox) {
		this.listbox = listbox;
	}

	/**
	 * @return the btTopo
	 */
	public Button getBtTopo() {
		return btTopo;
	}

	/**
	 * @param btTopo the btTopo to set
	 */
	public void setBtTopo(Button btTopo) {
		this.btTopo = btTopo;
	}

	/**
	 * @return the btBase
	 */
	public Button getBtBase() {
		return btBase;
	}

	/**
	 * @param btBase the btBase to set
	 */
	public void setBtBase(Button btBase) {
		this.btBase = btBase;
	}

	/**
	 * @return the btSubir
	 */
	public Button getBtSubir() {
		return btSubir;
	}

	/**
	 * @param btSubir the btSubir to set
	 */
	public void setBtSubir(Button btSubir) {
		this.btSubir = btSubir;
	}

	/**
	 * @return the btDescer
	 */
	public Button getBtDescer() {
		return btDescer;
	}

	/**
	 * @param btDescer the btDescer to set
	 */
	public void setBtDescer(Button btDescer) {
		this.btDescer = btDescer;
	}


	private void configurar() {
		this.setWidth("30px");
		this.setPack("center");
		this.setAlign("center");
		// this.setHeight((heigthListBox + 50) + "px");

		configurarBotoes();
		
		adicionarAcoesNosBotoes();

		//adicionando os botoes na Interface
		this.appendChild(new Separator());
		this.appendChild(new Separator());
		this.appendChild(btTopo);
		this.appendChild(new Separator());
		this.appendChild(btSubir);
		this.appendChild(new Separator());
		this.appendChild(btDescer);
		this.appendChild(new Separator());
		this.appendChild(btBase);

	}

	private void adicionarAcoesNosBotoes() {
		btTopo.addEventListener("onClick", new EventListener() {

			@Override
			public void onEvent(Event arg0) throws Exception {
				acaoMoverParaTopo(getListbox());

			}

		});

		btBase.addEventListener("onClick", new EventListener() {

			@Override
			public void onEvent(Event arg0) throws Exception {
				acaoMoverParaBase(getListbox());

			}

		});

		btSubir.addEventListener("onClick", new EventListener() {

			@Override
			public void onEvent(Event arg0) throws Exception {
				acaoSubir(getListbox());

			}

		});

		btDescer.addEventListener("onClick", new EventListener() {

			@Override
			public void onEvent(Event arg0) throws Exception {
				acaoDescer(getListbox());

			}

		});
		
	}

	private void configurarBotoes() {
		// botoes de ordenacao
		btTopo.setTooltiptext("Mover para o Topo");
		btTopo.setLabel("Topo");
		btTopo.setImage("/imagens/paraCima.png");

		btSubir.setTooltiptext("Subir");
		btSubir.setLabel("Subir");
		btSubir.setImage("/imagens/paraCima.png");

		btDescer.setTooltiptext("Descer");
		btDescer.setLabel("Descer");
		btDescer.setImage("/imagens/paraBaixo.png");

		btBase.setTooltiptext("Mover para a Base");
		btBase.setLabel("Base");
		btBase.setImage("/imagens/paraBaixo.png");

	}
	
	protected void acaoMoverParaBase(Listbox lboxSelecionados) {

		int i = 0;
		Iterator iterator = new LinkedHashSet(
				lboxSelecionados.getSelectedItems()).iterator();
		while (iterator.hasNext()) {
			Listitem selectedItem = (Listitem) iterator.next();
			lboxSelecionados.removeChild(selectedItem);
			lboxSelecionados.appendChild(selectedItem);
			lboxSelecionados.addItemToSelection(selectedItem);
		}

	}

	protected void acaoMoverParaTopo(Listbox lboxSelecionados) {
		int i = 0;
		Iterator iterator = new LinkedHashSet(
				lboxSelecionados.getSelectedItems()).iterator();
		while (iterator.hasNext()) {
			Listitem selectedItem = (Listitem) iterator.next();
			lboxSelecionados.removeChild(selectedItem);
			lboxSelecionados.insertBefore(selectedItem,
					lboxSelecionados.getItemAtIndex(i++));
			lboxSelecionados.addItemToSelection(selectedItem);

		}
	}

	protected void acaoDescer(Listbox lboxSelecionados) {

		Set<Listitem> selected = lboxSelecionados.getSelectedItems();
		if (selected.isEmpty())
			return;
		int index = lboxSelecionados.getIndexOfItem(selected.iterator().next());
		if (index == lboxSelecionados.getRows() - 1 || index < 0)
			return;
		Listitem selectedItem = lboxSelecionados.getItemAtIndex(index);
		lboxSelecionados.removeChild(selectedItem);
		lboxSelecionados.insertBefore(selectedItem,
				lboxSelecionados.getItemAtIndex(++index));
		lboxSelecionados.addItemToSelection(selectedItem);

	}

	protected void acaoSubir(Listbox lboxSelecionados) {

		Set<Listitem> selected = lboxSelecionados.getSelectedItems();
		if (selected.isEmpty())
			return;
		int index = lboxSelecionados.getIndexOfItem(selected.iterator().next());
		if (index == 0 || index < 0)
			return;
		Listitem selectedItem = lboxSelecionados.getItemAtIndex(index);
		lboxSelecionados.removeChild(selectedItem);
		lboxSelecionados.insertBefore(selectedItem,
				lboxSelecionados.getItemAtIndex(--index));
		lboxSelecionados.addItemToSelection(selectedItem);
	}

}
